Completed
Push — master ( dc50e4...8acfd5 )
by
unknown
51s
created

createAPI.js ➔ ... ➔ ???   B

Complexity

Conditions 1
Paths 2

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
c 1
b 0
f 0
nc 2
dl 0
loc 24
rs 8.9713
nop 2
1
import callAPIMethod from './callAPIMethod';
2
3
import applyMiddleware from './applyMiddleware';
4
5
const createAPI = (
6
    resources = {},
7
    middleware = [],
8
    APINamespace = '',
9
    fetchOptions = {}
10
) => Object.keys(resources).reduce( (api, resourceId) => {
11
    api[resourceId] = Object.keys(resources[resourceId].methods)
12
        .reduce( (resource, method) => {
13
            resource[method] = (params, methodOptions) => {
14
                const apiParams = resources[resourceId].methods[method](params);
15
                const boundCallAPIMethod = callAPIMethod.bind(
16
                    null,
17
                    APINamespace,
18
                    fetchOptions,
19
                    (resources[resourceId].namespace || resources[resourceId].prefix)
20
                );
21
                return applyMiddleware(
22
                    boundCallAPIMethod,
23
                    middleware,
24
                    methodOptions,
25
                    apiParams,
26
                    resourceId,
27
                    method
28
                );
29
            };
30
            return resource;
31
        }, {});
32
    return api;
33
}, {});
34
35
export default createAPI;